package SwingComponents;

import Connection.IPServer;
import Controllers.SelectWorldController;
import Entities.World;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.net.URL;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class SelectWorldPanel extends JPanel {

    private ArrayList<World> worldList;
    private ArrayList<IDButton> jButtonList;
    private SelectWorldController selectWorldController;
    private TransparentPanel beginPanel;
    private TransparentPanel centerPanel;
    private TransparentPanel endPanel;

    public SelectWorldPanel(SelectWorldController selectWorldController, ArrayList<World> worldList) {
        this.worldList = worldList;
        this.selectWorldController = selectWorldController;
        this.setLayout(new BorderLayout());
        this.setBackground(Color.BLACK);
        jButtonList = new ArrayList<>();
        initComponents();
    }

    private void initComponents() {
        beginPanel = new TransparentPanel();
        beginPanel.setSize(new Dimension(501, 100));
        beginPanel.setMaximumSize(new Dimension(501, 100));
        beginPanel.setMinimumSize(new Dimension(501, 100));
        beginPanel.setPreferredSize(new Dimension(501, 100));

        JLabel selectWorldLabel = new JLabel("Select World");
        selectWorldLabel.setFont(new Font("Dialog", Font.PLAIN, 24));
        selectWorldLabel.setForeground(Color.WHITE);

        beginPanel.add(selectWorldLabel);

        centerPanel = new TransparentPanel();
        centerPanel.setSize(new Dimension(501, 450));
        centerPanel.setMaximumSize(new Dimension(501, 450));
        centerPanel.setMinimumSize(new Dimension(501, 450));
        centerPanel.setPreferredSize(new Dimension(501, 450));
        centerPanel.setLayout(new GridLayout(3, 3, 1, 1));
        centerPanel.setBackground(Color.BLACK);

        endPanel = new TransparentPanel();
        endPanel.setSize(new Dimension(501, 35));
        endPanel.setMaximumSize(new Dimension(501, 35));
        endPanel.setMinimumSize(new Dimension(501, 35));
        endPanel.setPreferredSize(new Dimension(501, 35));

        JButton backButton = new JButton("Back");
        backButton.addActionListener(selectWorldController);

        endPanel.add(backButton);

        for (Integer i = 0; i < worldList.size(); i++) {
            IDButton auxButton = new IDButton(i, 30, 20, "World");
            auxButton.setSize(new Dimension(167, 181));
            auxButton.setMinimumSize(new Dimension(167, 181));
            auxButton.setMaximumSize(new Dimension(167, 181));
            auxButton.setPreferredSize(new Dimension(167, 181));
            jButtonList.add(auxButton);
            try {
                IPServer iPServer = new IPServer(selectWorldController.getPrincipalController().getApplication());
                jButtonList.get(i).setIcon(new ImageIcon(new URL(iPServer.getAdress() + worldList.get(i).getImage())));
            } catch (Exception ex) {
            }
            centerPanel.add(auxButton);
            jButtonList.get(i).addActionListener(selectWorldController);
        }
        if (jButtonList.size() % 9 != 0) {
            while (jButtonList.size() % 9 != 0) {
                IDButton auxButton = new IDButton(0, 0, 0, "");
                auxButton.setSize(new Dimension(167, 181));
                auxButton.setMinimumSize(new Dimension(167, 181));
                auxButton.setMaximumSize(new Dimension(167, 181));
                auxButton.setPreferredSize(new Dimension(167, 181));
                auxButton.setBackground(Color.BLACK);
                auxButton.setFocusPainted(false);
                auxButton.setBorderPainted(false);
                auxButton.setContentAreaFilled(false);
                jButtonList.add(auxButton);
                centerPanel.add(auxButton);
            }
        }
        this.add(beginPanel, BorderLayout.PAGE_START);
        this.add(centerPanel, BorderLayout.CENTER);
        this.add(endPanel, BorderLayout.PAGE_END);
    }

    public ArrayList<World> getWorldList() {
        return worldList;
    }
}
